Skip to content

fix(daemon): stop the lifetime-lock probe from dropping a held lock - #2207

Open
AmirF194 wants to merge 1 commit into
DeusData:mainfrom
AmirF194:fix/lifetime-lock-probe-drops-held-lock
Open

AmirF194 wants to merge 1 commit into
DeusData:mainfrom
AmirF194:fix/lifetime-lock-probe-drops-held-lock

Conversation

@AmirF194

Copy link
Copy Markdown
Contributor

While chasing #2162 I found a real, separate bug in posix_lifetime_lock_probe (src/daemon/ipc.c): when the in-process registry says this process already holds the lifetime lock, it still opens a throwaway fd on the lock file just to close it again and return 1. fcntl(2) record locks are scoped to (process, inode), not (fd, inode), so that close silently drops the real lock too, even though the reservation's own fd stays open.

Two probes back to back are enough to lose it. I wrote a test that acquires the reservation, probes it twice, then forks a genuinely separate process that tries to take the same lock directly. On main it succeeds, the lock is gone. With the fix it fails as expected.

Fix: check the in-process claim before opening anything and return early. That branch only ever needed to trust the registry, not touch the file.

scripts/test.sh --suites daemon_ipc: the new test fails on main (child_result == 1), 50 passed / 1 skipped with the fix.

Not a fix for #2162 itself, I ran into this while reading that code, it's a separate bug.

posix_lifetime_lock_probe opened a throwaway fd on the lock file to
confirm an already-claimed lock, then closed it. fcntl(2) record locks
are scoped to (process, inode), so that close silently released the
real lock too, even though the reservation's own fd stayed open.

Check the in-process claim before opening anything and return early;
that branch only ever needed to trust the registry.

Signed-off-by: Amir Fathi <amirfathi.me@gmail.com>
@AmirF194
AmirF194 requested a review from DeusData as a code owner September 14, 2026 14:10
@github-actions

Copy link
Copy Markdown

Thanks for opening this — it has been seen, and it is queued.

This note is automated, but it is not a brush-off: it exists so you know where your PR stands instead of having to guess from silence.

Current review status: working through a backlog. 0.9.1-rc.1 is out, so the release freeze that held reviews is over — but it left a large queue of open pull requests behind it, and we are reading through them oldest-first. The background is in discussion #1144.

What that means for this PR, concretely:

  • It will not be closed for inactivity. No stale bot touches pull requests here.
  • It may still sit a while before a human reads it. That is on us, not on you.
  • Older PRs are read first, so a recent one is not being skipped — it is behind a queue.

Things that will genuinely speed it up whenever review does happen:

  • Keep it rebased on main — the tree is moving quickly right now, and a conflicting branch cannot be reviewed as the diff you intended.
  • Get CI green, or say which failures you believe are pre-existing.
  • Keep the change to one claim. Bundled features and refactors get split before they get merged, which costs you a round trip.
  • Every commit needs a sign-off (git commit -s) — CI enforces DCO.

If this fixes a bug, a reproduction we can run is worth more than a description of the symptom.

Thanks for contributing, and sorry in advance for the wait.

@DeusData DeusData added bug Something isn't working stability/performance Server crashes, OOM, hangs, high CPU/memory security Security vulnerabilities, hardening priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. labels Sep 19, 2026
@DeusData

Copy link
Copy Markdown
Owner

Thank you for isolating the claim-lock descriptor path and providing a separate-process regression. This affects coordination between processes, so we need more time for the lifecycle review before making an integration decision. It should not be treated as proof that every cold-start race in #2162 is resolved.

@DeusData DeusData left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @AmirF194, for this one. It is a genuinely subtle catch, and the separate-process regression makes it easy to verify. Here is the lifecycle review we promised on the 19th.

The diagnosis is right. The lifetime lock is our only fcntl record lock, and POSIX releases those when any descriptor to the file is closed. The holder's own probe therefore opened a second descriptor, closed it, and quietly gave the lock away. Your early return fixes the probe. Windows is not affected: LockFileEx locks belong to a handle, and that path is not compiled there.

Two small asks before we merge:

  1. The sibling path in posix_lifetime_lock_try_acquire has the same pattern. When this process already holds the claim (process_result == 0), it calls private_regular_file_at_is_safe(endpoint->dir_fd, lock_name, 1) (src/daemon/ipc.c:1261 on current main). That helper opens and closes the lock file (ipc.c:848-857), so it drops the held lock exactly the way the probe did. Could you give it the same no-descriptor treatment, for example process_result == 0 && endpoint_runtime_still_valid(endpoint) ? 0 : -1? Please add a sibling test too: acquire, call try_acquire again in the same process (expect 0), and check that the child still cannot take the lock.
  2. Harden the child's verdict in the test. Report "held" only when open() succeeded and F_SETLK failed with EACCES/EAGAIN, and use a distinct value for anything else. At the moment any failure in the child, even a wrong path, looks like a pass.

Optional: a one-line comment at posix_record_lock_set stating the rule: never open and close the lifetime lock file in a process that may hold it, because fcntl locks are per process. The lock is fcntl rather than flock on purpose, so that forked children do not inherit it, which is exactly why the rule has to hold.

For the record: this hardens every process that holds the lifetime lock. The production daemon never probes its own lock, though, so this does not resolve the #2162 cold-start reports or the #2057 cold-storm flake. We will keep those open separately. Thanks again for tracking this down. Findings like this are what make the coordination layer trustworthy.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority/high Needs near-term maintainer attention; high-impact bug, regression, safety issue, or release blocker. security Security vulnerabilities, hardening stability/performance Server crashes, OOM, hangs, high CPU/memory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants